package com.example.excel.ui.activity.query;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.example.excel.R;
import com.example.excel.base.BaseActivity;
import com.example.excel.bean.InventoryBean;
import com.example.excel.databinding.ActivityAutoQueryBinding;
import com.example.excel.db.InventoryController;
import com.example.excel.utils.Utils;

import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
import per.goweii.actionbarex.common.OnActionBarChildClickListener;

public class AutoQueryActivity extends BaseActivity<ActivityAutoQueryBinding, InventoryViewModel> {

    private String mCode = "";

    public static void start(Context context) {
        Intent intent = new Intent(context, AutoQueryActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected Class<InventoryViewModel> initViewModel() {
        return InventoryViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_auto_query;
    }

    @Override
    protected void initView() {
        mBinding.setItemBean(new InventoryBean());
        mBinding.abc.setOnLeftIconClickListener(new OnActionBarChildClickListener() {
            @Override
            public void onClick(View v) {
                showFinishDialog();
            }
        });
        initScanner();


    }

    private void showFinishDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确定退出本次盘点吗？");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.show();
    }

    @Override
    protected void registerObserve() {
        super.registerObserve();
        mViewModel.mBean.observe(this, new Observer<InventoryBean>() {
            @Override
            public void onChanged(InventoryBean inventoryBean) {
                mCode = "";
                mBinding.etCode.setText(mCode);
                mBinding.etCode.setFocusable(true);
                mBinding.etCode.setFocusableInTouchMode(true);
                mBinding.etCode.requestFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromInputMethod(mBinding.etCode.getWindowToken(), 0);
                if (inventoryBean != null) {
                    inventoryBean.setFaFlag(1);
                    mBinding.setItemBean(inventoryBean);
                    mBinding.emptyLayout.setVisibility(View.GONE);
                    InventoryController.getInstance().update(inventoryBean);
                } else {
                    mBinding.emptyLayout.setVisibility(View.VISIBLE);
                    Utils.getInstance().showToast("盘点数据不存在");
                }
            }
        });
    }

    public void addOrScan() {
        String code = mBinding.etCode.getText().toString();
        if (!TextUtils.isEmpty(code)) {
            mViewModel.query(code);
        } else {
            Utils.getInstance().showToast("扫描为空");
        }

    }

    private void initScanner() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mBinding.etCode, 0);
        mBinding.etCode.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    String code = mBinding.etCode.getText().toString();
                    if (TextUtils.isEmpty(code)) {
                        Utils.getInstance().showToast("扫描为空");
                        mBinding.etCode.setFocusable(true);
                        mBinding.etCode.setFocusableInTouchMode(true);
                        mBinding.etCode.requestFocus();
                        return false;
                    }
                    mCode = code;
                    addOrScan();
                }
                return false;
            }
        });
        mBinding.etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (TextUtils.isEmpty(s.toString())) {
                    imm.hideSoftInputFromWindow(mBinding.etCode.getWindowToken(), 0);
                } else {
                    imm.showSoftInputFromInputMethod(mBinding.etCode.getWindowToken(), 0);
                    mBinding.etCode.setSelection(s.toString().length() - 1);
                }
            }
        });
        mCode = "";
        mBinding.etCode.setText(mCode);
        mBinding.etCode.setFocusable(true);
        mBinding.etCode.setFocusableInTouchMode(true);
        mBinding.etCode.requestFocus();
    }

    @Override
    public void onBackPressed() {
        showFinishDialog();
    }
}
